package org.softmed.rest.server.xstream;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class DateConverter implements SingleValueConverter {

	SimpleDateFormat parser = new SimpleDateFormat();

	public DateConverter() {
		parser.applyPattern("dd-MM-yyyy");
	}

	// @Override
	// public void marshal(Object arg0, HierarchicalStreamWriter arg1,
	// MarshallingContext arg2) {
	// String output = parser.format(arg0);
	// // TODO Auto-generated method stub
	// boolean nice = true;
	//
	// arg1.setValue(output);
	// // arg1.endNode();
	// // arg1.addAttribute("item-count", "" + list.size());
	// // arg1.addAttribute("uri", "oh dammnit!!!!");
	// // base uri + listName / fieldName - hot dammmn !!!!
	// }
	//
	// @Override
	// public Object unmarshal(HierarchicalStreamReader arg0,
	// UnmarshallingContext arg1) {
	//
	// Date object = null;
	// try {
	// object = parser.parse(arg0.getValue());
	// } catch (ParseException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return object;
	// }

	@Override
	public boolean canConvert(Class arg0) {
		return arg0 == Date.class;
	}

	@Override
	public Object fromString(String arg0) {
		Date object = null;
		try {
			object = parser.parse(arg0);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

	@Override
	public String toString(Object arg0) {
		String output = parser.format(arg0);
		return output;
	}

}
